import wepy from 'wepy'

export default class extends wepy.mixin {
  methods = {
    preivewPic(current) {
      wepy.previewImage({
        urls: this.goodsDetail.pics.map(item=> item.pics_mid),
        current
      })
    },
    onClickIcon() {
      wepy.switchTab({
        url: '/pages/tabs/cart'
      })
    },
    onClickButton() {
      this.$parent.addGoodsToCarts(this.goodsDetail)

      wepy.showToast({
        title: '添加购物车成功'
      })
    },
    async chooseAd() {
      let res = await wepy.chooseAddress().catch(e=>e)

      if(res.errMsg !== 'chooseAddress:ok')  return wepy.baseToast()

      this.addressInfo = res
      this.$apply()
    },
    onChange() {}
  }

  data = {
    id: '',
    goodsDetail: {},
    active: 0,
    addressInfo: ''
  }

  computed = {
    address: function() {
      let {addressInfo: info} = this.data

      if(info) {
        return info.provinceName + info.cityName + info.countyName
      }

      return '请选择收货地址'
    },
    allGoodsCount: function() {
      return this.$parent.globalData.allGoodsCount
    }
  }

  onLoad(options) {
    this.id = options.id

    this.getGoodsDetail()
  }

  async getGoodsDetail() {
    let { data: res } = await wepy.get('/goods/detail', { goods_id: this.id })

    if(res.meta.status !== 200) {
      return wepy.baseToast()
    }


    this.goodsDetail = res.message
    
    this.goodsDetail.goods_introduce = this.goodsDetail.goods_introduce.replace(/webp/ig, 'jpg')

    this.$apply()
  }
}